#!/bin/bash

shopt -s expand_aliases
alias sd='sudo -E env "PATH=$PATH:/usr/sbin:/sbin"'

download_as() {
    wget -nv $1 -O $2
}

download_to() {
    wget -nv $1 -P $2
}

TMP_FOLDER=/tmp/debian-conf/
rm -rf $TMP_FOLDER
mkdir -p $TMP_FOLDER

BASE_URL='https://gitee.com/zarekvane/debian-conf/raw/master'

curl -fsSL $BASE_URL/add_apt_repos.sh | bash -

sudo apt install -y bash-doc bash-completion psmisc apt-file
sudo apt install -y linux-headers-$(uname -r)
sudo apt install -y build-essential gdb gdb-doc
sudo apt install -y neofetch tree ranger lm-sensors
sudo apt install -y openssh-client openssh-server
sudo apt install -y net-tools curl proxychains4 v2ray
sudo apt install -y man-db doc-base debian-keyring
sudo apt install -y git git-doc subversion
sudo apt install -y zip unzip rar unrar p7zip-full
sudo apt install -y tmux gpm
sudo apt install -y vim vim-doc universal-ctags
sudo apt install -y llvm clang clang-format clangd
sudo apt install -y bear cmake cmake-doc ninja-build scons automake autoconf-doc
sudo apt install -y flex bison bison-doc
sudo apt install -y astyle highlight
sudo apt install -y python-is-python3 python3-pip python3-venv yapf3
sudo apt install -y openjdk-17-jdk maven
sudo apt install -y lua5.4
sudo apt install -y sqlite3 sqlite3-doc libsqlite3-dev
sudo apt install -y fzf gawk-doc
sudo apt install -y x11-xserver-utils imagemagick

sudo apt-file update

download_as $BASE_URL/clang-format $HOME/.clang-format
download_as $BASE_URL/gitconfig $HOME/.gitconfig

# install nodejs
NODE_VERSION=16.13.2
download_to "https://npmmirror.com/mirrors/node/v$NODE_VERSION/node-v$NODE_VERSION-linux-x64.tar.xz" $TMP_FOLDER
echo 'install nodejs...'
xz -d "$TMP_FOLDER/node-v$NODE_VERSION-linux-x64.tar.xz"
tar -xf "$TMP_FOLDER/node-v$NODE_VERSION-linux-x64.tar" -C "$TMP_FOLDER"
sudo rm -rf /opt/nodejs
sudo cp -rf "$TMP_FOLDER/node-v$NODE_VERSION-linux-x64" /opt/nodejs

# install golang
GO_VERSION=1.17.6
download_to https://golang.google.cn/dl/go$GO_VERSION.linux-amd64.tar.gz $TMP_FOLDER
echo 'install golang...'
sudo rm -rf /opt/go
sudo tar -C /opt/ -xzf "$TMP_FOLDER/go$GO_VERSION.linux-amd64.tar.gz"

download_as $BASE_URL/bashrc $HOME/.bashrc
download_as $BASE_URL/Xresources $HOME/.Xresources
download_as $BASE_URL/profile $HOME/.profile && source $HOME/.profile

if [ -d '/etc/v2ray/' ]; then
    download_as $BASE_URL/vvray_config.json $TMP_FOLDER/vv2ray_config.json
    sudo cp -f $TMP_FOLDER/vv2ray_config.json /etc/v2ray/config.json
fi

sudo systemctl restart v2ray.service

# config some mirrors
if [ -n `which pip3` ]; then
    sudo pip3 config --global set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
fi

if [ -n `which npm` ]; then
    npm config set registry https://registry.npm.taobao.org
    sd npm i -g yarn
    yarn config set registry https://registry.npm.taobao.org
    #sd npm install -g js-beautify
    #sd npm install -g typescript-formatter
    #sd npm install -g lua-fmt
fi

if [ -n `go` ]; then
    export GO111MODULE=on
    export GOPROXY=https://goproxy.cn

    #go get -u mvdan.cc/sh/cmd/shfmt
    #go get -u github.com/klauspost/asmfmt/cmd/asmfmt
fi

rm -rf $TMP_FOLDER

